<template>
<div class="vue-box">
  <div class="c-panel">
    <!-- ------------- 数据列表 ------------- -->
    <el-table ref="data-table" class="data-table" :data="dataList" border stripe>
      <sa-td name="序号" type="text" prop="sort" />
      <sa-td name="条目" type="text" prop="name" />
      <sa-td name="编码" type="text" prop="code" />
      <sa-td type="img" name="图标" prop="icon" />
      <sa-td type="enum" name="启用状态" prop="status" :jv="dims.YES_OR_NO" />
      <sa-td name="操作" fixed="right">
        <template slot-scope="s">
          <div class="oper">
            <el-button type="text" @click="updateItem(s.row)">
              变更
            </el-button>
            <el-button type="text" @click="delItem(s.row)">
              <span style="color: red;">
                删除
              </span>
            </el-button>
          </div>
        </template>
      </sa-td>
    </el-table>
    <!-- 分页 -->
    <div v-if="dataCount">
      <sa-item type="page" :curr.sync="p.current" :size.sync="p.size" :total="dataCount" @change="f5()" />
    </div>
  </div>
</div>
</template>

<script>
export default {
  name: 'items-list',
  props: {
    param: Object
  },
  data() {
    return {
      // 查询参数
      p: {
        current: 1,
        size: 10,
        dictId: this.param.id
      },
      dataCount: null,

      // 字典项
      dataList: null,

      // 字典id
      dictId: this.param.id,

      // 缓存字典
      dims: sa.$sys.getCurd(),
    }
  },
  created: function () {
    this.f5();
    // 监听表单动作
    sa.onInputEnter();
  },
  watch: {
    'p.current': {
      handler(n, _o) {
        this.p.current = n
        this.f5()
      },
      immediate: true
    }
  },
  methods: {
    // 刷新
    f5() {
      sa.ajax('/dict/item/getList', sa.removeNull(this.p), function (res) {
        if (res.code == 200) {
          // 数据
          this.dataList = res.data;
          // 数据总数
          this.dataCount = res.dataCount;
          // 刷新表格高度
          sa.f5TableHeight();
        }
      }.bind(this), {
        type: "post"
      });
    },

    // 删除
    delItem: function (data) {
      sa.confirm('是否删除，此操作不可撤销', function () {
        sa.ajax('/dict/item/delete?id=' + data.id, function (res) {
          sa.ok(res.msg);
          this.f5()
        }.bind(this), {
          type: 'get'
        })
      }.bind(this));
    },

    // 修改子项目
    updateItem(row) {
      // 弹出新增页面
      sa.showModel('变更', () => import('./item-add'), row);
      sa.f5TableHeight(); // 刷新表格高度
    }
  }
}
</script>

<style scoped>
.dict_item {
  display: flex;
  justify-content: space-between;
}

.dict,
.item {
  width: 49%;
}

.item {
  height: 100%;
}

::v-deep .td-img {
  width: 20px;
  height: 20px;
}

.oper {
  display: flex;
  justify-content: space-evenly;
}
</style>
